﻿Imports System.Reflection
Namespace Win
    Public Class WinCtrlBase
        ''' <summary>
        ''' Create an intance of a Win, So we can implement Factory Pattern on our Win Class
        ''' </summary>
        ''' <param name="factory"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CreateInstanceOf(ByVal typeOfCtrl As WinCtrlTypeOf, Optional ByVal factory As Boolean = True) As XtraUserControl
            'How to Load a class from an assembly
            'http://msdn.microsoft.com/en-us/library/wccyzw83.aspx
            'Imports System
            'Class DynamicInstanceList
            '            Private Shared instanceSpec As String = "System.EventArgs;System.Random;" + _
            '                "System.Exception;System.Object;System.Version"

            '            Public Shared Sub Main()
            '                Dim instances() As String = instanceSpec.Split(";")
            '                Dim instlist As Array = Array.CreateInstance(GetType(Object), instances.Length)
            '                Dim item As Object

            '                For i As Integer = 0 To instances.Length - 1
            '                    ' create the object from the specification string
            '                    Console.WriteLine("Creating instance of: {0}", instances(i))
            '                    item = Activator.CreateInstance(Type.GetType(instances(i)))
            '                    instlist.SetValue(item, i)
            '                Next i
            '                Console.WriteLine(vbNewLine + "Objects and their default values:" + vbNewLine)
            '                For Each o As Object In instlist
            '                    Console.WriteLine("Type:     {0}" + vbNewLine + "Value:    {1}" + _
            '                        vbNewLine + "HashCode: {2}" + vbNewLine, _
            '                        o.GetType().FullName, o.ToString(), o.GetHashCode())
            '                Next o
            '            End Sub
            'End Class



            'Find where the assembly is
            Dim assemblyUri As Uri = New Uri(Reflection.Assembly.GetExecutingAssembly().CodeBase)
            'Get the path
            Dim currentPath As String = IO.Path.GetDirectoryName(assemblyUri.LocalPath)
            Dim typeFound As Boolean = False
            '
            'Default if there is no SDK.
            Dim sdkList As String = "SystemBoxWinSDK.dll"
            Dim instanceWin As XtraUserControl = Nothing
            '
            Dim pluginAssembly As Assembly
            Dim assemblyTypes As System.Type()
            '
            'Configured Default WinSDK
            If Not String.IsNullOrWhiteSpace(AppManager.DefaultWinSDK) Then
                sdkList = AppManager.DefaultWinSDK
            End If
            '
            'Configured Client WinSDK when we use Factory
            If factory Then
                If Not String.IsNullOrWhiteSpace(AppManager.ClientWinSDK) Then
                    sdkList = String.Format("{0},{1}", AppManager.ClientWinSDK, sdkList)
                End If

            End If
            '
            For Each sdk As String In sdkList.Split(","c)
                For Each file As String In IO.Directory.GetFiles(currentPath, sdk)
                    'load the assembly file
                    pluginAssembly = Reflection.Assembly.LoadFrom(file)
                    'Get all then class in the assembly
                    assemblyTypes = pluginAssembly.GetTypes()
                    'Scan to get the class in the assembly
                    For Each type As System.Type In assemblyTypes
                        'Check if Class found, make sure the class name is unique for all NameSpace
                        If type.Name = typeOfCtrl.ToString Then
                            'Create the object as instantePL, if the type found is of type PLBase
                            instanceWin = TryCast(Activator.CreateInstance(type, False), XtraUserControl)
                            If instanceWin IsNot Nothing Then
                                typeFound = True
                                Exit For 'For Speed Purpose we for the exit For
                            End If
                        End If
                    Next
                    If typeFound = True Then Exit For 'For Speed Purpose we for the exit For
                Next
                If typeFound = True Then Exit For 'For Speed Purpose we for the exit For
            Next
            '
            Return instanceWin
        End Function
    End Class
End Namespace
